import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
//import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.PreparedStatement;
import java.util.Properties;

public class DataStorage {
	// private String driver;
	private String url;
	private String user;
	private String pass;

	private Connection con;
	private PreparedStatement pst_ranking;
	PreparedStatement pst_char;

	DataStorage() {
		con = null;
	}

	public void init(String paramFile) throws FileNotFoundException,
			IOException {
		// Use Properties to load attributes
		Properties props = new Properties();
		props.load(new FileInputStream(paramFile));
		// driver = props.getProperty("driver");
		url = props.getProperty("url");
		user = props.getProperty("user");
		pass = props.getProperty("pass");

		try {
			con = DriverManager.getConnection(url, user, pass);
			if (con != null) {
				pst_ranking = con
						.prepareStatement("INSERT INTO ranking(shortname,fullname,date,category,ranking,downloads,headline,description,last_updated,screenshot,recommended) VALUES(?,?,?,?,?,?,?,?,?,?,?)");
				pst_char = con
						.prepareStatement("INSERT INTO characteristics(name,date,likes,dislikes,updated,windows,mac,linux,bsd,categories,registered,license,languages,audience,ui,programming_language,awards) VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)");
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			con = null;
			e.printStackTrace();
		}
	}

	public boolean isConnected() {
		return !(con == null);
	}

	public boolean insertProjectDetails(String pname, String name, String date,
			String category, Integer ranking, Integer downloads,
			String description, String detailedDescription, String updated,
			String screenshot, String recommended) throws Exception {

		if (con == null) {
			throw (new Exception("Connection not established!"));
		}
		try {

			pst_ranking.setString(1, pname);
			pst_ranking.setString(2, name);
			pst_ranking.setString(3, date);
			pst_ranking.setString(4, category);
			pst_ranking.setInt(5, ranking);
			pst_ranking.setInt(6, downloads);
			pst_ranking.setString(7, description);
			pst_ranking.setString(8, detailedDescription);
			pst_ranking.setString(9, updated);
			pst_ranking.setString(10, screenshot);
			pst_ranking.setString(11, recommended);
			return pst_ranking.executeUpdate() == 1;
		} catch (SQLException e) {
			throw e;
		}
	}

	public boolean insertProjectPageDetails(String pname, String date,
			Integer like, Integer dislike, String updated, Integer has_window,
			Integer has_mac, Integer has_linux, Integer has_bsd,
			String categories, String registered, String license,
			String languages, String audience, String ui, String pl,
			String awards) throws Exception {
		if (con == null) {
			throw (new Exception("Connection not established!"));
		}
		try {

			pst_char.setString(1, pname);
			pst_char.setString(2, date);
			pst_char.setInt(3, like);
			pst_char.setInt(4, dislike);
			pst_char.setString(5, updated);
			pst_char.setInt(6, has_window);
			pst_char.setInt(7, has_mac);
			pst_char.setInt(8, has_linux);
			pst_char.setInt(9, has_bsd);
			pst_char.setString(10, categories);
			pst_char.setString(11, registered);
			pst_char.setString(12, license);
			pst_char.setString(13, languages);
			pst_char.setString(14, audience);
			pst_char.setString(15, ui);
			pst_char.setString(16, pl);
			pst_char.setString(17, awards);
			return pst_char.executeUpdate() == 1;
		} catch (SQLException e) {
			throw e;
		}
	}

	public boolean insertStaffPick(String category, String date,
			Integer ranking, String name, String url) throws Exception {
		if (con == null) {
			throw (new Exception("Connection not established!"));
		}
		try {
			PreparedStatement pst = con
					.prepareStatement("INSERT INTO staffpick(category,date,ranking,name,url) VALUES(?,?,?,?,?)");
			pst.setString(1, category);
			pst.setString(2, date);
			pst.setInt(3, ranking);
			pst.setString(4, name);
			pst.setString(5, url);
			return pst.executeUpdate() == 1;
		} catch (SQLException e) {
			throw e;
		}
	}

}
